/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ficha;

import control.Skill;
import ficha.storyteller.vampiro.AreaFicha;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.DefaultListSelectionModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

/**
 *
 * @author yurifw
 */
public class SkillTable extends JPanel{
    private JTable table;
    private JButton btnAdiciona;
    private JButton btnRemove;
    private JScrollPane scroll;
    private AreaFicha areaFicha;
    
    public SkillTable(AreaFicha areaFicha) {
        this.areaFicha = areaFicha;
        init();
        adicionaListeners();
        criaUI();
    }

    private void init() {
        table = new JTable(new SkillTableModel());
        table.setShowVerticalLines(false);
        table.getColumnModel().getColumn(1).setCellRenderer(new SkillCellRenderer(areaFicha));
        btnAdiciona = new JButton(new ImageIcon(getClass().getResource("/ficha/imagens/icon-add.png")));
        btnAdiciona.setPreferredSize(new Dimension(30, 30));
        btnRemove = new JButton(new ImageIcon(getClass().getResource("/ficha/imagens/icon-cross.png")));
        btnRemove.setPreferredSize(new Dimension(30, 30));
        scroll = new JScrollPane(table);
    }

    private void adicionaListeners() {
        btnAdiciona.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((SkillTableModel)table.getModel()).inserir();
            }
        });
        btnRemove.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((SkillTableModel)table.getModel()).excluir(table.getSelectedRows());
            }
        });
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                JTable tb = (JTable)e.getSource();
                int row = tb.rowAtPoint(e.getPoint());
                int col = tb.columnAtPoint(e.getPoint());
                
                if (col==1){
                    int nivelAtual = (int)tb.getValueAt(row, col);
                    if (e.getButton()==e.BUTTON1 && nivelAtual<10){
                        tb.setValueAt(++nivelAtual, row, col);
                    } else if (e.getButton()==e.BUTTON3 && nivelAtual>0){
                        tb.setValueAt(--nivelAtual, row, col);
                    }
                }
                tb.repaint();
            }
        });
    }

    private void criaUI() {
        this.setOpaque(false);
        scroll.setOpaque(false);
        table.setOpaque(false);
        table.getColumnModel().getColumn(1).setPreferredWidth(80);
        BoxLayout b = new BoxLayout(this, BoxLayout.Y_AXIS);
        this.setLayout(b);
        this.add(scroll);
        JPanel buttons = new JPanel(new BorderLayout());
        buttons.add(btnAdiciona,BorderLayout.WEST);
        buttons.add(btnRemove,BorderLayout.EAST);
        buttons.setOpaque(false);
        this.add(buttons);
    }
    
    public List<Skill> getSkills (){
        return ((SkillTableModel)table.getModel()).getSkills();
    }
    
    public void limpar(){
        ((SkillTableModel)table.getModel()).limpar();
    }
    
    public void inserir(Skill s ){
        ((SkillTableModel)table.getModel()).inserir(s);
    }
    
    
    
}
